#include "mainwindow.h"
#include "ui_mainwindow.h"
#include<QIcon>
#include<QPixmap>
#include<QPalette>


//文件系统
#include<QDir>
#include<QFile>
#include<QFileInfo>

#include<QMessageBox>

#include<time.h>
MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    ,m_mode(ORDER_MODE),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    setFixedSize(1020,720);
    QString title="音乐播放器";
    setWindowTitle(title);
    m_player=new QMediaPlayer(this);
    QString musicPath="音乐路径";
    m_player->setMedia(QUrl::fromLocalFile(musicPath));
    // m_player->play();
    QString musicDir="音乐文件夹";
    loadAppointMusicDir(musicDir);
    setBackground(":/images/background2.png");

    srand(time(NULL));
    initButtons();
}
void MainWindow::loadAppointMusicDir(QString & filepath){

    QDir dir(filepath);
    if(dir.exists()==false){
        QMessageBox::warning(this,"文件夹","音乐列表为空");
        return;
    }
    QFileInfoList fileList=dir.entryInfoList(QDir::Files);

    for(auto element :fileList){
        if(element.suffix()=="mp3"){
            ui->musicList->addItem(element.baseName());
        }
    }

    ui->musicList->setCurrentRow(0);
}
void MainWindow::handleModeSlot(){
    if(m_mode==ORDER_MODE){
        m_mode=RANDOM_MODE;
        ui->modeBtn->setIcon(QIcon(":/Icon/random.png"));
    }
    else if(m_mode==RANDOM_MODE ){
        m_mode=CIRCLE_MODE;
        ui->modeBtn->setIcon(QIcon(":/Icon/random.png"));
    }
    else if(m_mode==CIRCLE_MODE){
        m_mode=ORDER_MODE;
        ui->modeBtn->setIcon(QIcon(":/Icon/order.png"));
    }
}
void MainWindow::handlePlaySlot(){
    if(m_player->state()==QMediaPlayer::PlayingState){
        m_player->pause();
        ui->playBtn->setIcon(QIcon(":/Icon/play.png"));
    }
    else{
        m_player->play();
        ui->playBtn->setIcon(QIcon(":/Icon/stop.png"));
    }
}
void MainWindow:: setButtonStyle(QPushButton * button,const QString & filename){

    button->setFixedSize(50,50);
    button->setIcon(QIcon(filename));
    button->setStyleSheet("background-color:transparent");//透明色
    button->setIconSize(QSize(button->width(),button->height()));
}
void MainWindow::initButtons(){
    setButtonStyle(ui->preBtn,":/Icon/PreSong.png");
    setButtonStyle(ui->modeBtn,":/Icon/order.png");
    setButtonStyle(ui->playBtn,":/Icon/play.png");
    setButtonStyle(ui->nextBtn,":/Icon/NextSong.png");
    setButtonStyle(ui->listBtn,":/Icon/music.png");

    connect(ui->playBtn,&QPushButton::clicked,this,&MainWindow::handlePlaySlot);
    connect(ui->modeBtn,&QPushButton::clicked,this,&MainWindow::handleModeSlot);
    connect(ui->nextBtn,&QPushButton::clicked,this,&MainWindow::handleNextSlot);
}
void  MainWindow::handleNextSlot(){
    int currentRow=ui->musicList->currentRow();
    int nextRow=0;
    if(m_mode==ORDER_MODE){
        nextRow=(currentRow+1)%ui->musicList->count();

    }
    else if(m_mode==CIRCLE_MODE){
        nextRow=currentRow;
    }else if (m_mode==RANDOM_MODE){
        int cnt=0;
        do{

        rand()%ui->musicList->count();
        }while((cnt<=3)&&(currentRow==nextRow));
    }
    ui->musicList->setCurrentRow(nextRow);
}
void MainWindow::setBackground(const QString & filename){
    QPixmap pixmap(filename);
    QSize windowSize=this->size();
    //适配大小
    QPixmap scalePixmap=pixmap.scaled(windowSize,Qt::IgnoreAspectRatio,Qt::SmoothTransformation);
    QPalette palette=this->palette();
    //设置背景照片
    palette.setBrush(QPalette::Background,QBrush(scalePixmap));
    //应用到窗口
    this->setPalette(palette);
}

MainWindow::~MainWindow()
{
    delete ui;
}
